package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Reference
    private UserService userService;

    @Reference
    private MenuService menuService;

    @RequestMapping("getUsername")
    public Result getUsername(Principal principal){

        try {
            //获取当前登陆的用户名
            String name = principal.getName();

            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,name);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }

    }

//    以下内容为新增

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = userService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return  new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }

    }
    @RequestMapping("findAllRole")
    public Result findAllRole(){
        try {
            List<Role> list = userService.findAllRole();
            return  new Result(true,"查询角色数据成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色数据失败");
        }
    }
    @RequestMapping("add")
    public Result add(@RequestParam("roleIds")Integer[] roleIds, @RequestBody User user){
        try {
            userService.add(roleIds,user);
            return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }
    }
    @RequestMapping("findRoleIdsByUserId")
    public Result findRoleIdsByUserId(@RequestParam("id")Integer id){
        try {
           List<Integer> roleIds = userService.findRoleIdsByUserId(id);
            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
    @RequestMapping("edit")
    public Result edit(@RequestParam("roleIds")Integer [] roleIds,@RequestBody User user){

        try {
            userService.edit(roleIds,user);
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }

    }
    @RequestMapping("testUsername")
    public Result testUsername(@RequestParam("username")String username){

           Integer count = userService.testUsername(username);
            if(count==0){
                return new Result(true,"用户名不存在");
            }else {
                return new Result(false,"用户名已存在");
            }
    }
    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id")Integer id){
        try {
            userService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    /**
     * 根据用户名获取自己私有的页面
     * @param username 指定用户名
     * @return List<Menu>
     */
    @RequestMapping("getPersonallyPages")
    public Result getPersonallyPages(@RequestParam("username")String username){
        try {
            if (username == null){
                return new Result(false, MessageConstant.GET_MENU_FAIL);
            }
            List<Menu> list = menuService.getPersonallyPages(username);
            HashMap<String, Object> map = new HashMap<>();
            // 寻找数组里有html的
            String initLinkUrl = list.get(0).getChildren().get(0).getLinkUrl();
            map.put("personallyPages",list);
            map.put("initLinkUrl",initLinkUrl);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

}
